/**
   AdminsControlller
 * Time:2018年5月25日
 */
package com.ssm.controller;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Condition;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.util.CollectionUtils;
import com.ssm.entity.Admins;
import com.ssm.service.AdminsService;

/**
 * @author 15199
 *
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/admin")
public class AdminsControlller {

	@Resource
	private AdminsService adminsService;

	@RequestMapping("/login.do")
	public ModelAndView login(String account,String password,String checkcode,HttpServletRequest request){
		Admins admins=new Admins(Integer.parseInt(account),Integer.parseInt(password));

		String code=(String)request.getSession().getAttribute("CheckCode");

		if(code.equals(checkcode)){
			List <Admins> list=adminsService.checkLogin(admins);
			if(list.size()>0){
				ModelAndView mav=new ModelAndView("/AE/index");
				request.getSession().setAttribute("account",list.get(0).getAdmijnaccount());
				request.getSession().setAttribute("password",list.get(0).getAdminpassword());
				return mav;
			}else{
				ModelAndView mav=new ModelAndView("/AE/Login");
				return mav;
			}
		}else{
			ModelAndView mav=new ModelAndView("/AE/Login");
			return mav;
		}
	}

	@RequestMapping("/check.do")
	@ResponseBody
	public String check(@RequestBody Admins admins,HttpServletRequest request,HttpServletResponse response){

		List<Admins> list=adminsService.checkLogin(admins);
		if(CollectionUtils.isEmpty(list)){
			return "false";
		}else{
			return "true";                                                                                                                                                               
		}



	}
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(@RequestBody Admins admins,HttpServletRequest request,HttpServletResponse response){
		//		System.out.println(admins.getAdmijnaccount()+"         "+admins.getAdminpassword());
		int n=adminsService.updatePassword(admins);
		if(n>0){
			return "true";
		}else{
			return "false";
		}


	}









}
